package com.bringway.p2p.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;
import com.bringway.framework.dao.ZhfwJdbcDao;
import com.bringway.global.util.StringUtils;
import com.bringway.p2p.dao.MenuCUDDao;
import net.sf.json.JSONObject;

/*
 * 
 * @author fan
 * @className MenuCUDDaoImpl
 * @description 菜单增删改的持久层dao实现类
 * @date 2015年12月14日下午6:55:45
 */
@Repository
public class MenuCUDDaoImpl extends ZhfwJdbcDao implements MenuCUDDao {
	Logger logger=Logger.getLogger(MenuCUDDaoImpl.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bringway.p2p.dao.MenuCUDDao#addMenu(net.sf.json.JSONObject)
	 */
	@Override
	public int addMenu(JSONObject parms) {
		// String id = parms.optString("id");
		String name = parms.optString("name");
		String url = parms.optString("url");
		String params = parms.optString("params");
		String parent_id = parms.optString("parent_id");
		String px = parms.optString("px");
		String sql = "INSERT INTO d_ccb_fm.t_fm_sys_menu (NAME, URL, PARAMS, PARENT_ID, ORDER_NUM, CREATE_DATE) VALUES (:NAME,:URL,:PARAMS,:PARENT_ID,:ORDER_NUM,now())";
		SqlParameterSource parm = new MapSqlParameterSource().addValue("NAME", name).addValue("URL", url).addValue("PARAMS", params)
				.addValue("PARENT_ID", parent_id).addValue("ORDER_NUM", px);
		// parm.put("id", id);
		logger.info("addMenu sql is "+sql.toString());
		logger.info("addMenu parms is "+parms);
		return this.getNamedParameterJdbcTemplate().update(sql, parm);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bringway.p2p.dao.MenuCUDDao#deleteMenu(net.sf.json.JSONObject)
	 */
	@Override
	public int deleteMenu(JSONObject parms) {
		int id = parms.optInt("id");
		String sql = "DELETE FROM d_ccb_fm.t_fm_sys_menu WHERE ID= :id";
		SqlParameterSource paramMap = new MapSqlParameterSource().addValue("id", id);
		return this.getNamedParameterJdbcTemplate().update(sql, paramMap);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bringway.p2p.dao.MenuCUDDao#updateMenu(net.sf.json.JSONObject)
	 */
	@Override
	public int updateMenu(JSONObject parms) {
		int id = parms.optInt("id");
		int parent_id = parms.optInt("parent_id", 0);
		String name = parms.optString("name");
		String url = parms.optString("url");
		String params = parms.optString("params");
		String px = parms.optString("px");
		StringBuffer sql = new StringBuffer();
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		parmsMap.put("id", id);
		sql.append(" UPDATE d_ccb_fm.t_fm_sys_menu SET id = :id ");

		if (StringUtils.isNotEmpty(name)) {
			sql.append(" ,name = :name ");
			parmsMap.put("name", name);
		}
		if (StringUtils.isNotEmpty(url)) {
			sql.append(" ,url = :url");
			parmsMap.put("url", url);
		}
		if (StringUtils.isNotEmpty(params)) {
			sql.append(" ,params = :params ");
			parmsMap.put("params", params);
		}
		if (parent_id != 0) {
			sql.append(" ,parent_id = :parent_id ");
			parmsMap.put("parent_id", parent_id);
		}
		if (StringUtils.isNotEmpty(px)) {
			sql.append(" ,order_num = :px ");
			parmsMap.put("px", px);
		}
		sql.append(" WHERE id = :id ");
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), parmsMap);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bringway.p2p.dao.MenuCUDDao#query(java.lang.String,
	 * java.util.Map)
	 */
	@Override
	public List<Map<String, Object>> query(String sql, Map<String, Object> parmsMap) {
		return this.getNamedParameterJdbcTemplate().queryForList(sql, parmsMap);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bringway.p2p.dao.MenuCUDDao#getPageCount(java.lang.String,
	 * java.util.HashMap)
	 */
	@Override
	public int getPageCount(String sql, Map<String, Object> parmsMap) {
		return this.getNamedParameterJdbcTemplate().queryForObject(sql, parmsMap, Integer.class);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.dao.MenuCUDDao#updateMenuUser(net.sf.json.JSONObject, java.util.Map)
	 */
	@Override
	public int updateMenuUser(String sql, Map<String, String> parmsMap) {
		return this.getNamedParameterJdbcTemplate().update(sql,parmsMap);
	}

	@Override
	public List queryMenuRoleList(JSONObject params) {
		String menuId = params.optString("menuId");;
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		StringBuffer sql = new StringBuffer();
		sql.append(" select t1.ID,t1.NAME,t1.PARENT_ID from t_fm_sys_role t1 where t1.ID in (select t2.ROLE_ID from t_fm_sys_menu_role t2 where t2.MENU_ID = :MENU_ID ) ");
		paramsMap.put("MENU_ID", menuId);
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), paramsMap);
	}

	@Override
	public void MenuRoleRelatinShipDel(JSONObject params) {
		String menuId = params.optString("menuId");
		StringBuffer sql = new StringBuffer();
		sql.append(" DELETE FROM d_ccb_fm.t_fm_sys_menu_role WHERE MENU_ID=:menuId ");
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("menuId", menuId);
		this.getNamedParameterJdbcTemplate().update(sql.toString(),paramsMap);
	}

	@Override
	public int MenuRoleRelatinShipSave(JSONObject params) {
		String MENU_ID = params.optString("MENU_ID");
		String ROLE_ID = params.optString("ROLE_ID");
		String ROLE_NAME = params.optString("ROLE_NAME");
		Map<String,Object> paramsMap = new HashMap<String,Object>();
		paramsMap.put("MENU_ID", MENU_ID);
		paramsMap.put("ROLE_ID", ROLE_ID);
		paramsMap.put("ROLE_NAME", ROLE_NAME);
		StringBuffer sql = new StringBuffer();
		sql.append(" INSERT INTO d_ccb_fm.t_fm_sys_menu_role (MENU_ID, ROLE_ID, ROLE_NAME)"
				+ " VALUES (:MENU_ID,:ROLE_ID,:ROLE_NAME) ");
		return this.getNamedParameterJdbcTemplate().update(sql.toString(), paramsMap);
	}

	@Override
	public List<Map<String, Object>> queryTreeMenuList(JSONObject params) {
		String username=params.optString("username");
		StringBuffer sql=new StringBuffer();
		sql.append("select ID,NAME,PARENT_ID,case when id in(select t1.MENU_ID from t_fm_sys_user_menu t1 ");
		sql.append("left join t_fm_sys_user t2 on t1.user_id=t2.id where t2.gyh=:username) then true ");
		sql.append( "when id in(select MENU_ID from t_fm_sys_menu_role where role_id in(select t1.ROLE_ID ");
		sql.append( "from t_fm_sys_user_role t1 left join t_fm_sys_user t2 on t1.USER_ID=t2.ID where t2.GYH=:username))");
		sql.append(" then true else false end disabled from t_fm_sys_menu order by ORDER_NUM ;");
		Map<String, Object> paramMap=new HashMap<>();
		paramMap.put("username", username);
		logger.info("queryTreeMenuList sql is "+sql.toString());
		logger.info("queryTreeMenuList paramMap is "+paramMap);
		return this.getNamedParameterJdbcTemplate().queryForList(sql.toString(), paramMap);
	}
}
